import Vue from "vue";
import VueRouter from "vue-router";
import Home from "@/views/home/index.vue";
import Layout from "@/layout/index.vue";
import NotFound from "@/views/404/404.vue";
import store from "@/store";
import zbData from "@/views/zbData/index.vue";
import tblog from "@/views/tblog/index.vue";
import Appearances from "@/views/Appearances/index.vue";
import realTimeAppearances from "@/views/realTimeAppearances/index.vue";
import realTimeApproach from "@/views/realTimeApproach/index.vue";
import Individual from "@/views/individual/index.vue";
import bindLicensePlate from "@/views/bindLicensePlate/index.vue";
import Login from "@/views/login/index.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/login",
    name: "Login",
    component: Login,
    meta: {
      title: "登录",
    },
  },
  {
    path: "/",
    redirect: "/login", // 默认重定向到登录页
  },
  {
    path: "/home",
    component: Layout,
    meta: {
      title: "供应商投标",
      loading: true,
    },
    redirect: "/zbData", // 默认重定向到订单信息页面
    children: [
      {
        path: "/home",
        name: "Home",
        component: Home,
        meta: {
          transition: "fade",
          title: "中标公告",
          icon: "fas fa-comments",
        },
      },
      {
        path: "/zbData",
        name: "ZbData",
        component: zbData,
        meta: {
          transition: "fade",
          title: "招标信息",
          icon: "fas fa-comments",
        },
      },
      {
        path: "/tblog",
        name: "Tblog",
        component: tblog,
        meta: {
          transition: "fade",
          title: "投标记录",
          icon: "fas fa-comments",
        },
      },
    ],
  },
  {
    path: "*",
    component: NotFound,
    meta: {
      title: "404 - 页面未找到",
    },
  },
];

const router = new VueRouter({
  mode: "history",
  base: "/",
  routes,
  scrollBehavior(to, from, savedPosition) {
    return { x: 0, y: 0 };
  },
});

// 解决重复点击导航时，控制台出现报错
const VueRouterPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch((err) => err);
};

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  //关闭搜索框
  store.commit("SET_SEARCH_VISIBLE", false);
  next();
});

export default router;
